<?php
/**
 * Galaxy - Classe de gestion du monde de jeu
 *
 * @author Staff Centaurius
 * @date 26 Dec 2007
 **/
class Galaxy {

	/**
	 * Vérifie si les coordonées galaxies passées sont valides
	 *
	 * @param int $sys_x
	 * @param int $sys_y
	 * @return bool (faux si la coordonnée galaxie n'est pas valide)
	 */
	static function checkGalaxCoord($sys_x, $sys_y) {

		$isvalid = true;

		if ($sys_x <= 0 OR $sys_x > _GALAX_SIZE_X_COEF) $isvalid = false;
		if ($sys_y <= 0 OR $sys_y > _GALAX_SIZE_Y_COEF) $isvalid = false;

		return $isvalid;
	}

	/**
	 * Vérifie si les coordonées secteur passées sont valides
	 *
	 * @param int $pos_x
	 * @param int $pos_y
	 * @return bool (faux si la coordonnée secteur n'est pas valide)
	 */
	static function checkSectCoord($pos_x, $pos_y) {

		$isvalid = true;

		if ($pos_x <= 0 OR $pos_x > _SECT_SIZE_X) $isvalid = false;
		if ($pos_y <= 0 OR $pos_y > _SECT_SIZE_Y) $isvalid = false;

		return $isvalid;
	}


	/**
	 * Retourne la liste des systemes
	 *
	 * @return array $res
	 **/
	static function getSystemesListe() {

		$sql = "SELECT s.sys_id, s.sys_nom, s.sys_owner, a.astr_etat
				FROM d_systeme s
				LEFT JOIN d_astre a
					ON a.astr_sys_x = s.sys_pos_x
					AND a.astr_sys_y = s.sys_pos_y
					AND a.astr_type = 1
				GROUP BY s.sys_id";
		$req = SQLManager::query($sql);
		$res = SQLManager::fetch_all($req);

		return $res;
	}

	/**
	 * Retourne les information d'un systeme
	 *
	 * @return array $res
	 **/
	static function getSysteme($sys_x, $sys_y) {

		$sql = "SELECT sys_id, sys_nom, sys_owner
				FROM d_systeme
				WHERE sys_pos_x = $sys_x AND sys_pos_y = $sys_y LIMIT 1";
		$req = SQLManager::query($sql);
		$res = SQLManager::fetch_array($req);

		return $res;
	}

	/**
	 * Retourne la liste des astres d'un secteur
	 *
	 * @return array $res
	 **/
	static function getSecteurAstres($sys_x, $sys_y) {

		$sql = "SELECT a.astr_id, a.astr_nom, a.astr_pos_x, a.astr_pos_y, a.astr_type,
					 a.astr_image, u.usr_pseudo, u.usr_id, f.fact_color
				FROM d_astre a
				LEFT JOIN d_user u
					ON u.usr_id = a.usr_id_ext
				LEFT JOIN d_user_faction uf
					ON uf.usr_id_ext = u.usr_id
					AND fact_rank_id_ext > 0
				LEFT JOIN d_faction f
					ON f.fact_id = uf.fact_id_ext
				WHERE astr_sys_x = $sys_x AND astr_sys_y = $sys_y";
		$req = SQLManager::query($sql);
		$res = SQLManager::fetch_all($req);

		return $res;
	}

	/**
	 * Calcule le niveau de vision d'un joueur dans le secteur
	 *
	 * @param int $usr_id
	 * @param int $sys_x
	 * @param int $sys_y
	 * @return int $spy_lvl (niveau de vision dans le secteur)
	 **/
	static function getSecteurSpyLvl($usr_id, $sys_x, $sys_y) {

		$spy_lvl = 0;
		$tour_max = array();

		$sql = "SELECT a.astr_id, ab.bat_id_ext, MAX(ab.bat_lvl) as bat_lvl_max
				FROM d_astre a
				JOIN d_astre_batiment ab
					ON ab.astr_id_ext = a.astr_id
				JOIN s_batiment b
					ON b.bat_id = ab.bat_id_ext
				WHERE a.usr_id_ext = $usr_id AND b.bat_type = 'information'
					AND (a.astr_sys_x = $sys_x AND a.astr_sys_y = $sys_y)
					GROUP BY ab.bat_id_ext";
		$req = SQLManager::query($sql);
		$tour_max = SQLManager::fetch_array($req);

		//si il y a une tour au joueur ds le secteur
		if (isset($tour_max['bat_lvl_max']) AND $tour_max['bat_lvl_max'] > 0) {

			//Selon le niveau de la tour
			if ($tour_max['bat_lvl_max'] == 1) {

				$spy_lvl = 1;
			} elseif ($tour_max['bat_lvl_max'] > 1 AND $tour_max['bat_lvl_max'] <= 4) {

				$spy_lvl = 2;
			} elseif ($tour_max['bat_lvl_max'] > 4) {

				$spy_lvl = 3;
			}
		}

		if ($spy_lvl == 3) {
			return $spy_lvl; //Si tour niveau max
		}

		$sql_s = "SELECT f.flt_id
				FROM d_flotte f
				JOIN s_unite u
					ON u.unt_id = f.unt_id_ext
				WHERE f.usr_id_ext = $usr_id AND u.unt_type = 'espion'
					AND (f.flt_sys_x = $sys_x AND f.flt_sys_y = $sys_y)
					AND (f.flt_action = 0 OR f.flt_action = 2)
					LIMIT 1";
		$req_s = SQLManager::query($sql_s);
		$sonde = SQLManager::fetch_array($req_s);

		if (isset($sonde['flt_id']) AND $sonde['flt_id'] > 0) {

			//Si une sonde est trouvé dans le secteur
			//$spy_lvl = 3;
			$sql = "SELECT a.astr_id, ab.bat_id_ext, MAX(ab.bat_lvl) as bat_lvl_max
					FROM d_astre a
					JOIN d_astre_batiment ab
						ON ab.astr_id_ext = a.astr_id
					JOIN s_batiment b
						ON b.bat_id = ab.bat_id_ext
					WHERE a.usr_id_ext = $usr_id AND b.bat_type = 'information'
						GROUP BY ab.bat_id_ext";
			$req = SQLManager::query($sql);
			$tour_max = SQLManager::fetch_array($req);

			//si le joueur a une tour dans la galaxie
			if (isset($tour_max['bat_lvl_max']) AND $tour_max['bat_lvl_max'] > 0) {

				//Selon le niveau de la tour
				if ($tour_max['bat_lvl_max'] == 1) {

					$spy_lvl = 1;
				} elseif ($tour_max['bat_lvl_max'] > 1 AND $tour_max['bat_lvl_max'] <= 4) {

					$spy_lvl = 2;
				} elseif ($tour_max['bat_lvl_max'] > 4) {

					$spy_lvl = 3;
				}
			}
		}

		return $spy_lvl;
	}

	/**
	 * Retourne la liste des flotte d'un secteur
	 *
	 * @param int $sys_x
	 * @param int $sys_y
	 * @param int $spy_lvl (niveau de visibilité)
	 * @return array $flt
	 **/
	static function getSecteurFlotte($sys_x, $sys_y, $usr_id, $spy_lvl = 0) {

		$flt = array();
		$spy_query = "";
		$spy_query_sel = "";

			if ($spy_lvl == 0) {
				$spy_query .= " AND us.usr_id = $usr_id ";
			}

			if ($spy_lvl < 2) {
				$spy_query_sel .= ", '' as nbr_flt";
			} else {
				$spy_query_sel .= ", SUM(f.flt_nb_unt) as nbr_flt";
			}

			if ($spy_lvl < 3) {
				$spy_query .= " AND u.unt_type <> 'espion' ";
			}

			//TODO grouper par faction quand on les aura implémentée
			$sql = "SELECT f.flt_pos_x, f.flt_pos_y $spy_query_sel
					FROM d_flotte f
						JOIN s_unite u
							ON u.unt_id = f.unt_id_ext
						JOIN d_user us
							ON us.usr_id = f.usr_id_ext
					WHERE f.flt_sys_x = $sys_x AND f.flt_sys_y = $sys_y
						AND (f.flt_action = 0 OR f.flt_action = 2 OR f.flt_action = 3)
					$spy_query
					GROUP BY CONCAT(f.flt_pos_x, f.flt_pos_y)";

			Log::message('<b>Requette H</b> : ' . $sql);

			$req = SQLManager::query($sql);
			$flt = SQLManager::fetch_all($req);

		return $flt;
	}

	/**
	 * Retourne la liste et les info des flotte se trouvant a une adresse
	 *
	 * @param int $usr_id
	 * @param int $sys_x
	 * @param int $sys_y
	 * @param int $pos_x
	 * @param int $pos_y
	 * @param int $spy_lvl (niveau de visibilité)
	 **/
	static function searchFlotteToAdress($usr_id, $sys_x, $sys_y, $pos_x, $pos_y, $spy_lvl = 0) {

		$flt = array();
		$spy_query = "";
		$spy_query_sel = "";

		if ($spy_lvl == 0) {
			$spy_query .= " AND f.usr_id_ext = $usr_id ";
		}

		if ($spy_lvl < 2) {
			$spy_query_sel .= ", '' as flt_nb_unt";
		} else {
			$spy_query_sel .= ", f.flt_nb_unt";
		}

		if ($spy_lvl < 3) {
			$spy_query .= " AND u.unt_type <> 'espion' ";
		}

		//TODO grouper par faction quand on les aura implémentée
		$sql = "SELECT f.flt_id, u.lang_unt_nom, u.unt_id, us.usr_id, us.usr_pseudo, us.usr_avatar,
						ua.alli_id_ext, fa.fact_color, fa.fact_nom, f.flt_action $spy_query_sel
				FROM d_flotte f
					JOIN s_unite u
						ON u.unt_id = f.unt_id_ext
					JOIN d_user us
						ON us.usr_id = f.usr_id_ext
					LEFT JOIN d_user_alliance ua
						ON ua.usr_id_ext = f.usr_id_ext
					LEFT JOIN d_user_faction uf
						ON uf.usr_id_ext = us.usr_id
						AND fact_rank_id_ext > 0
					LEFT JOIN d_faction fa
						ON fa.fact_id = uf.fact_id_ext
				WHERE f.flt_pos_x = $pos_x AND f.flt_pos_y = $pos_y
					AND f.flt_sys_x = $sys_x AND f.flt_sys_y = $sys_y
					AND ((u.unt_mobile = 0 AND (f.flt_action = 0 OR f.flt_action = 2 OR f.flt_action = 3)) OR u.unt_mobile = 1)
					$spy_query
				ORDER BY us.usr_id ASC, u.unt_mobile ASC";

		$req = SQLManager::query($sql);
		$flt = SQLManager::fetch_all($req);

		return $flt;
	}

	/**
	 * Retourne la liste et les info des combats se déroulant a une adresse
	 *
	 * @param int $sys_x
	 * @param int $sys_y
	 * @param int $pos_x
	 * @param int $pos_y
	 **/
	static function getAdressFight($sys_x, $sys_y, $pos_x, $pos_y) {

		$flt = array();

		$sql = "SELECT fi.fgt_id, fi.fgt_start_time, ff.fgt_team, fi.fgt_string_id,
						u.usr_id, u.usr_pseudo
				FROM d_fight fi
				JOIN d_fight_flotte ff
					ON ff.fgt_id_ext = fi.fgt_id
				JOIN d_flotte f
					ON f.flt_id = ff.flt_id_ext
				JOIN d_user u
					ON f.usr_id_ext = u.usr_id
				WHERE fi.fgt_winner_team = 0
					AND (f.flt_pos_x = $pos_x AND f.flt_pos_y = $pos_y
						AND f.flt_sys_x = $sys_x AND f.flt_sys_y = $sys_y)
				GROUP BY u.usr_id
				ORDER BY fi.fgt_id DESC, ff.fgt_team ASC";

		$req = SQLManager::query($sql);
		$flt = SQLManager::fetch_all($req);

		return $flt;
	}

	/**
	 * Crée un favoris
	 *
	 * @param unknown_type $usr_id
	 * @param unknown_type $fav_nom
	 * @param unknown_type $sys_x
	 * @param unknown_type $sys_y
	 * @param unknown_type $pos_x
	 * @param unknown_type $pos_y
	 * @return unknown
	 */
	static function createFavoris($usr_id, $fav_nom, $sys_x, $sys_y, $pos_x, $pos_y) {
		$sql = "INSERT INTO d_user_favoris VALUES('', $fav_nom, $usr_id,  $sys_x, $sys_y, $pos_x, $pos_y)";
		$req = SQLManager::query($sql);

		return true;
	}

	/**
	 * Supprime un favoris
	 *
	 * @param unknown_type $usr_id
	 * @param unknown_type $fav_id
	 */
	static function deleteFavoris($usr_id, $fav_id) {
		$sql = "DELETE FROM d_user_favoris WHERE fav_id = $fav_id AND usr_id_ext = $usr_id";
		$req = SQLManager::query($sql);

		return true;
	}

	/**
	 * Récupere la liste des favoris d'un joueur
	 *
	 * @param unknown_type $usr_id
	 * @return unknown
	 */
	static function getFavorisListe($usr_id) {
		$sql = "SELECT fav_nom, fav_sys_x, fav_sys_y, fav_pos_x, fav_pos_y FROM d_user_favoris WHERE usr_id_ext = $usr_id";
		$req = SQLManager::query($sql);
		$res = SQLManager::fetch_all($req);

		return $res;
	}
}

?>
